package org.example.myleet.p824;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class Solution {

    private static final HashSet<Character> VOWEL_SET = new HashSet<>(5);

    static {
        VOWEL_SET.add('a');
        VOWEL_SET.add('e');
        VOWEL_SET.add('i');
        VOWEL_SET.add('o');
        VOWEL_SET.add('u');
        VOWEL_SET.add('A');
        VOWEL_SET.add('E');
        VOWEL_SET.add('I');
        VOWEL_SET.add('O');
        VOWEL_SET.add('U');
    }

    public String toGoatLatin(String sentence) {
        List<String> result = new ArrayList<>(150);
        StringBuilder word = new StringBuilder();
        char[] sentenceCharArr = sentence.toCharArray();
        int i = 1, x = 0;
        while (x < sentenceCharArr.length || word.length() > 0) {
            char c = x >= sentenceCharArr.length ? ' ' : sentenceCharArr[x];
            if (c == ' ') {
                if (!VOWEL_SET.contains(word.charAt(0))) {
                    word.append(word.charAt(0));
                    word.deleteCharAt(0);
                }
                word.append('m').append('a');
                for (int j = 0; j < i; ++j) {
                    word.append('a');
                }
                result.add(word.toString());
                word = new StringBuilder();
                ++i;
            } else {
                word.append(c);
            }
            ++x;
        }
        return String.join(" ", result);
    }
}
